<?php
declare (strict_types = 1);

namespace app\home\controller;

use app\home\business\GoodsBusiness;
use think\facade\View;
use think\Request;

class Goods extends Base
{
    /**
     * 商品列表
     */
    public function list(){

        $cate_id = \request()->get('cate_id',0);
        if (!is_numeric($cate_id)) {
            return redirect('/');
        }
        // 排序的字段数据、不再数据中、直接跳转到首页/可以使用默认排序
        $sortField = [
            'sort','sales_num','is_new','is_hot','is_recommend'
        ];
        $sort_field = request()->get('sort_field','sort');
        if (!in_array($sort_field,$sortField)) {
            return redirect('/');
        }
        # 获取商品的列表
        $goodsList = GoodsBusiness::list($cate_id,$sort_field);
        View::assign('goodsList',$goodsList);
        View::assign('sort_field',$sort_field);
        return View::fetch();
    }

    /**
     * 详细
     * @return string
     */
    public function detail(){
        $id  =\request()->get('id');
        if (!is_numeric($id)) {
            return redirect('/');
        }
        # 查询商品数据
        $detail = GoodsBusiness::detail($id);
        View::assign('detail',$detail);
        return View::fetch();
    }
}
